/**
 * author:Administrator
 * date:2016/3/14
 * desc: 填充模拟数据
 */

'use strict';
let co = require('co');
//
let roleService = require('./application/roleService.js');
let cmdService = require('./application/cmdService.js');
let queryService = require('./application/queryService.js');

/******************************************************
 *                    添加用户                         *
 ******************************************************/
let user = {name: 'wzq', pwd: 'wzq'};
let roles = {
  manager: {name: 'root'},
  vendor: {name: 'vendor1'},
  customer: {name: 'customer1'},
  keeper: {name: 'keeper1'}
};

function* addUser() {
  let user1 = yield roleService.registerUser(user.name, user.pwd);
  if (!user1) throw new Error('registerUser fail');
  let uId = user1.id;

  let mgr = yield roleService.registerManager({uId: uId, name: roles.manager.name});
  let vd = yield roleService.registerVendor({uId: uId, name: roles.vendor.name});
  let cust = yield roleService.registerCustomer({uId: uId, name: roles.customer.name});
  let kp = yield roleService.registerKeeper({uId: uId, name: roles.keeper.name});
  console.log('user ', user);
  console.log('mgr ', mgr);
  console.log('vd ', vd);
  console.log('cust ', cust);
  console.log('kp ', kp);

  console.log(' [x] addUser success');
}

/******************************************************
 *                    添加位置                         *
 ******************************************************/
// 荔湾区、海珠区、黄埔区、花都区、萝岗区、番禺区、南沙区
let locations = [
  {
    name: '广州',
    child: [
      {name: '天河', child: [{name: '岑村'}, {name: '车陂'}, {name: '程介村'}]},
      {name: '越秀', child: [{name: '百灵路'}, {name: '北京路'}]},
      {name: '白云'}]
  }
];

let locations_result = [];
function* addLocation() {
  let list = [].concat(locations);

  while (list.length > 0) {
    let loc = list.pop();
    console.log('save loc', loc.name, loc.pid);
    let pLoc = yield cmdService.createLocation(loc.name, loc.pid);
    locations_result.push(pLoc);

    if (loc.child && loc.child.length > 0) {
      for (let c of loc.child) {
        c.pid = pLoc.id;
        list.push(c);
      }
    }
  }
  console.log(' [x] addLocation success');
}

/******************************************************
 *                    添加类别                         *
 ******************************************************/
let categories = [
  {
    name: '美容',
    child: [
      {name: '祛痘', child: [{name: '医学祛痘'}]},
      {name: '祛斑', child: [{name: '医学祛斑'}]}
    ]
  }, {
    name: '皮肤中心',
    child: [
      {name: '激光脱毛'},
      {name: '痕疤修复'}
    ]
  }
];

let categories_result = [];
function* addCategory() {
  let list = [].concat(categories);

  while (list.length > 0) {
    let cate = list.pop();
    console.log('save cate', cate.name, cate.pid);
    let pCate = yield cmdService.createCategory(cate.name, cate.pid);
    categories_result.push(pCate);
    if (cate.child && cate.child.length > 0) {
      for (let c of cate.child) {
        c.pid = pCate.id;
        list.push(c);
      }
    }
  }
  console.log(' [x] addCategory success');
}

co(function*() {
  yield addUser();
  yield addLocation();
  yield addCategory();

  console.log(' [x] ****** all done ******')
}).catch(function (err) {
  console.log(err.stack);
});
